﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.Expressions;
using BUS;
using Telerik.Web.UI;
using OOP;
using System.Web.Script.Services;
using DAL;
using OPP;
namespace WebApplication2
{
    public partial class DemoGrid : System.Web.UI.Page
    {
        static ArcustomerRepository ar = new ArcustomerRepository();
        static string rootpath;
        static int i = 0,j=0;
        static List<ARCUSTOMER> list = new List<ARCUSTOMER>();
        static List<string> loadtow;
        string rootprovince, rootdist;
        TownReposity tw = new TownReposity();
        ProvinceReposity pro = new ProvinceReposity();
        List<Town> t = new List<Town>();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            grvStudentDetails.DataSource = list;
            if (!IsPostBack)
            {
                i = 0;
                j = 0;
                list = new List<ARCUSTOMER>();
                grvStudentDetails.DataBind();
                Customer_Bus cust = new Customer_Bus();
                loadprovince();
                //check name
                rootpath = Server.MapPath("book.xml");
                if (FileExists(rootpath) == false || ar.update(rootpath) == true)
                        ar.GhiVaoXMl(rootpath);
             
            }
         
            
        }
        public void loadprovince()
        {
            DropDownList province;
             DropDownList dist;
            Customer_Bus cust = new Customer_Bus();
            TownReposity town = new TownReposity();
            ProvinceReposity provin = new ProvinceReposity();
            if (j > 0)
            {
                
                province = grvStudentDetails.FooterRow.FindControl("dlprovince_add") as DropDownList;
                province.DataSource = provin.addprovincetown(Server.MapPath("Province.xml"));
                province.DataTextField = "ProvinceName";
                province.DataValueField = "ProvinceId";
                province.DataBind();
                dist = grvStudentDetails.FooterRow.FindControl("dltown_add") as DropDownList;
                dist.DataSource = town.addlisttown(Server.MapPath("town.xml"), 1);
                dist.DataTextField = "DistrictName";
                dist.DataValueField = "DistrictName";
                dist.DataBind();
            }
            else
            {
                province = grvStudentDetails.Controls[0].Controls[0].FindControl("dlprovinceemp") as DropDownList;
                province.DataSource = provin.addprovincetown(Server.MapPath("Province.xml"));
                province.DataTextField = "ProvinceName";
                province.DataValueField = "ProvinceId";
                province.DataBind();
                dist = grvStudentDetails.Controls[0].Controls[0].FindControl("dldistemp") as DropDownList;
                dist.DataSource = town.addlisttown(Server.MapPath("town.xml"), 1);
                dist.DataTextField = "DistrictName";
                dist.DataValueField = "DistrictName";
                dist.DataBind();
            }
            j++;
        }
        //autocomplete
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<string> loadtown(string towncode)
        {
            List<string> result = ar.Search(rootpath, towncode);
            return result;
        }
        //end autocomplete
        protected void btnadd_click(object sender, EventArgs e)
        {

            ARCUSTOMER c = new ARCUSTOMER();
            TextBox custid, custname, address, channel, ngay;
            CheckBox active;
            DropDownList province, town;
            bool act;
            string[] a;
            a = new string[3];
           
            if (i > 0)
            {
                custid = grvStudentDetails.FooterRow.FindControl("txtcust_add") as TextBox;
                custname = grvStudentDetails.FooterRow.FindControl("txtcustname_add") as TextBox;
                address = grvStudentDetails.FooterRow.FindControl("txtaddress_add") as TextBox;
                province = grvStudentDetails.FooterRow.FindControl("dlprovince_add") as DropDownList;
                town = grvStudentDetails.FooterRow.FindControl("dltown_add") as DropDownList;
                channel = grvStudentDetails.FooterRow.FindControl("txtchannel_add") as TextBox;
                active = grvStudentDetails.FooterRow.FindControl("txtactive_add") as CheckBox;
                ngay = grvStudentDetails.FooterRow.FindControl("txtngayadd") as TextBox;
                c.CustID = custid.Text;
                c.Customername = custname.Text;
                c.Address = address.Text;
                c.ProvinceCode = province.SelectedItem.Text;
                c.TownCode = town.SelectedItem.Text;
                c.Channel = Convert.ToInt32(channel.Text);
                if (active.Checked == true)
                    act = true;
                else
                    act = false;
                c.Active = act;
                a = ngay.Text.Split(new Char[] { '/' });
                string chuoicat = a[1] + "/" + a[0] + "/" + a[2];
                c.Ngay = Convert.ToDateTime(chuoicat);
                list.Add(c);
            }
            else
            {

                custid = (TextBox)grvStudentDetails.Controls[0].Controls[0].FindControl("txtcustidemp");
                custname = grvStudentDetails.Controls[0].Controls[0].FindControl("txtcustnameemp") as TextBox;
                address = grvStudentDetails.Controls[0].Controls[0].FindControl("txtcustaddressemp") as TextBox;
                province = grvStudentDetails.Controls[0].Controls[0].FindControl("dlprovinceemp") as DropDownList;
                town = grvStudentDetails.Controls[0].Controls[0].FindControl("dldistemp") as DropDownList;
                channel = grvStudentDetails.Controls[0].Controls[0].FindControl("txtchanemp") as TextBox;
                active = grvStudentDetails.Controls[0].Controls[0].FindControl("txtactivemp") as CheckBox;
                ngay = grvStudentDetails.Controls[0].Controls[0].FindControl("txtngay") as TextBox;
                c.CustID = custid.Text;
                c.Customername = custname.Text;
                c.Address = address.Text;
                c.ProvinceCode = province.SelectedItem.Text;
                c.TownCode = town.SelectedItem.Text;
                c.Channel = Convert.ToInt32(channel.Text);
                if (active.Checked == true)
                    act = true;
                else
                    act = false;
                c.Active = act;
                a = ngay.Text.Split(new Char[] { '/' });
                string chuoicat = a[1] + "/" + a[0] + "/" + a[2];
                c.Ngay = Convert.ToDateTime(chuoicat);
                list.Add(c);
            }
            grvStudentDetails.DataBind();
            loadprovince();
    
            i++;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        protected void grvStudentDetails_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grvStudentDetails.EditIndex = e.NewEditIndex;
            grvStudentDetails.DataBind();
        }

        protected void grvStudentDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvStudentDetails.EditIndex = -1;
            grvStudentDetails.DataBind();
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            list = new List<ARCUSTOMER>();
            i = 0;
            j = 0;
            grvStudentDetails.DataSource = list;
            grvStudentDetails.DataBind();
            loadprovince();
        }

        protected void grvStudentDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void dlprovin_emp_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList dist, province;
            int pro = 0;
            TownReposity town = new TownReposity();
            province = grvStudentDetails.Controls[0].Controls[0].FindControl("dlprovinceemp") as DropDownList;
            dist = grvStudentDetails.Controls[0].Controls[0].FindControl("dldistemp") as DropDownList;
            pro = Convert.ToInt32(province.SelectedValue);
            dist.DataSource = town.addlisttown(Server.MapPath("town.xml"), pro);
            dist.DataTextField = "DistrictName";
            dist.DataValueField = "DistrictName";
            dist.DataBind();
        }
        protected void dlprovince_add_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList dist, province;
            TownReposity town = new TownReposity();
            int pro = 0;
            province = grvStudentDetails.FooterRow.FindControl("dlprovince_add") as DropDownList;
                dist = grvStudentDetails.FooterRow.FindControl("dltown_add") as DropDownList;
               pro=Convert.ToInt32(province.SelectedValue);
               dist.DataSource = town.addlisttown(Server.MapPath("town.xml"), pro);
               dist.DataTextField = "DistrictName";
               dist.DataValueField = "DistrictName";
                dist.DataBind();  
        }

        private bool FileExists(string root)
        {
            try
            {
                return (System.IO.Directory.Exists(root));  //Exception for folder
            }
            catch (Exception)
            {
                return (false);                                   //Error occured, return False
            }
            return true;
        }
    }
}